﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
namespace WPFExcelReport
{
    public partial class TGLLG
    {
        #region ==EXCLUDEWH===========
        public static readonly DependencyProperty EXCLUDEWHProperty = DependencyProperty.Register("EXCLUDEWH",
            typeof(string), typeof(TGLLG),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEXCLUDEWHChanged));
        public string EXCLUDEWH
        {
            get
            {
                return (string)GetValue(EXCLUDEWHProperty);
            }
            set
            {
                SetValue(EXCLUDEWHProperty, value);
            }
        }
        private static void OnEXCLUDEWHChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGLLG;
            if (src != null)
            {
                src.EXCLUDEWH = (string)e.NewValue;
            }
        }
        #endregion

        #region ==LOTTYPE===========
        public static readonly DependencyProperty LOTTYPEProperty = DependencyProperty.Register("LOTTYPE",
            typeof(string), typeof(TGLLG),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnLOTTYPEChanged));
        public string LOTTYPE
        {
            get
            {
                return (string)GetValue(LOTTYPEProperty);
            }
            set
            {
                SetValue(LOTTYPEProperty, value);
            }
        }
        private static void OnLOTTYPEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGLLG;
            if (src != null)
            {
                src.LOTTYPE = (string)e.NewValue;
            }
        }
        #endregion

        #region ==dwReq===========
        public static readonly DependencyProperty dwReqProperty = DependencyProperty.Register("dwReq",
        typeof(DataRow), typeof(TGLLG),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwReqChanged));

        public DataRow dwReq
        {
            get
            {
                return (DataRow)GetValue(dwReqProperty);
            }
            set
            {
                SetValue(dwReqProperty, value);
                LOADBYREQ();
            }
        }
        private static void OdwReqChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGLLG;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwReq = row;
            }
        }
        #endregion

        #region ==dwGD===========
        public static readonly DependencyProperty dwGDProperty = DependencyProperty.Register("dwGD",
        typeof(DataRow), typeof(TGLLG),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwGDChanged));

        public DataRow dwGD
        {
            get
            {
                return (DataRow)GetValue(dwGDProperty);
            }
            set
            {
                SetValue(dwGDProperty, value);
                TMV.RESETREQ(value);
            }
        }
        private static void OdwGDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGLLG;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwGD = row;
            }
        }
        #endregion
        #region ==FTREQ===========
        public static readonly DependencyProperty FTREQProperty = DependencyProperty.Register("FTREQ",
            typeof(bool), typeof(TGLLG),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFTREQChanged));
        public bool FTREQ
        {
            get
            {
                return (bool)GetValue(FTREQProperty);
            }
            set
            {
                SetValue(FTREQProperty, value);
                ((VGLLG)MV).FTREQ = value;
            }
        }
        private static void OnFTREQChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGLLG;
            if (src != null)
            {
                src.FTREQ = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==ALLCOATE===========
        public static readonly DependencyProperty ALLCOATEProperty = DependencyProperty.Register("ALLCOATE",
            typeof(bool), typeof(TGLLG),
            new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnALLCOATEChanged));
        public bool ALLCOATE
        {
            get
            {
                return (bool)GetValue(ALLCOATEProperty);
            }
            set
            {
                SetValue(ALLCOATEProperty, value);
                ((VGLLG)MV).ALLCOATE = value;
            }
        }
        private static void OnALLCOATEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGLLG;
            if (src != null)
            {
                src.ALLCOATE = (bool)e.NewValue;
            }
        }
        #endregion  

        public TGLLG()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VGLLG();
            DataContext = MV;
            InitializeComponent();
            EXCLUDEWH = "01HMA";
        }

       
        public void LOADBYREQ()
        {
            try
            {
                if (!IsAuto)
                    return;
                TBMain.Clear();

                if (dwReq == null)
                {
                    return;
                }
                if (dwReq["GAART"].ToString().StartsWith("2"))
                {
                    LOTTYPE = "染批";
                }
                if (dwReq["GAART"].ToString().StartsWith("1"))
                {
                    LOTTYPE = "纺批";
                    if (!dwReq["GLAUFMSL"].ToString().StartsWith("SM"))
                    {
                        LOTTYPE = "染批";
                    }
                }
                LO = dwReq["LO"].ToString();
                LB = dwReq["LB"].ToString();
                LP = dwReq["LP"].ToString();
                dwReq["ADDT"] = EXCLUDEWH;
                DataSet SResult;
                if ((LO + LB + LP.Trim()).StartsWith(EXCLUDEWH))
                {
                    SResult = ((VGLLG)MV).QueryGLLG(dwReq, 5);
                }
                else
                {
                    SResult = ((VGLLG)MV).QueryGLLG(dwReq, 2);
                }
                MergeData(SResult, true, true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void OnAutoChg(bool value)
        {
            if (!IsAuto)
                return;
            LOADBYREQ();
            base.OnAutoChg(value);
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }

        private void btnRemoveAllocate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                TMV.RVZUTEI(dwGD);
                TMV.RESETREQ(dwGD);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnAllocate_Click(object sender, RoutedEventArgs e)
        {
            Allocate(0);
        }
        public void Allocate(double QTYR = 0)
        {
            try
            {
                if (dwGD == null)
                {
                    MessageBox.Show("请选择要保存的纱线!");
                    return;
                }
                if (dwGB == null)
                {
                    MessageBox.Show("请选择要留位的记录!");
                    return;
                }
                TMV.GLLGALLOCATE(dwGD, dwGB, QTYR);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPartialAllocate_Click(object sender, RoutedEventArgs e)
        {
            if (QTY == 0)
            {
                return;
            }
            Allocate(QTY);
            double GAKG1 = Convert.ToDouble(dwGD["GAKG1"].ToString()); 
            double GAKG0 = Convert.ToDouble(dwGD["GAKG0"].ToString());
            double GASP1 = Convert.ToDouble(dwGD["GASP1"].ToString());
            dwGD["GAKG1"] = GAKG1 - GAKG0;
            dwGD["GAKG0"] = 0;
            double RATIO = Helper.RoundX((GAKG1 - GAKG0)*100 / GAKG1,1000);
            dwGD["GASP1"] = Helper.RoundX(GASP1 * RATIO*0.01*0.1,1)*10;
            TMV.VGD.UGD26(dwGD);
            dwData = dwGD.Table.NewRow(); 
            dwData.ItemArray = dwGD.ItemArray;
            var gapos = (from DataRow row in dwGD.Table.AsEnumerable()
                         select row["GAPOS"].ToString())
                         .Max();
            int POS = Convert.ToInt16((string)gapos);
            dwData["GAPOS"] = Helpers.Helper.Right("00" + (POS + 1), 2);
            dwData["GAKG2"] = 0;
            dwData["GAKG3"] = 0;
            dwData["GAKG5"] = 0;
            dwData["GAKG1"] = GAKG0;
            dwData["GABEM"] = "".PadRight(40);
            dwData["GASP1"] = Helper.RoundX(GASP1*(1 - RATIO * 0.01)*0.1,1)*10;
            dwData["GAKG0"] = GAKG0; 
            dwData["GANEUBEN"] = app.User;
            dwData["GANEUDAT"] = MV.date;
            TMV.VGD.DeleteGAAUF(dwData, 1);
            TMV.VGD.InsertGAAUF(dwData, app.company);
            dwGD.Table.Rows.InsertAt(dwData, 0);
            QTY = 0;
        }
      
    }
}
